import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../generated/assets.dart';

/// Created by zfz on 2025/1/7.
/// 通用标题栏
class WJNavBar extends StatefulWidget implements PreferredSizeWidget {
  final double? contentHeight; // 从外部指定高度
  final Color? backgroundColor; // 设置导航栏背景的颜色
  final Color? textColor; //标题字体颜色
  final Widget? leadingWidget; //左边组件
  final String? leadingImg; // 左边图片
  final Widget? trailingWidget; // 右边组件
  final String? title; // 中间文字
  final TextStyle? titleStyle;
  final Function? onBack; // 返回事件
  final bool? showBackButton; // 是否展示

  const WJNavBar({
    super.key,
    this.contentHeight,
    this.backgroundColor = Colors.transparent,
    this.textColor = Colors.black,
    this.leadingWidget,
    this.leadingImg,
    this.trailingWidget,
    this.title,
    this.titleStyle,
    this.onBack,
    this.showBackButton = true,
  });

  @override
  State<StatefulWidget> createState() => _MkTopNavigationBarState();

  @override
  Size get preferredSize => Size.fromHeight(ScreenUtil().statusBarHeight + (contentHeight ?? 52.w));
}

class _MkTopNavigationBarState extends State<WJNavBar> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.backgroundColor,
      height: ScreenUtil().statusBarHeight + (widget.contentHeight ?? 52.w),
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Column(
        children: [
          SizedBox(height: ScreenUtil().statusBarHeight),
          Container(
            width: ScreenUtil().screenWidth,
            height: widget.contentHeight ?? 52.w,
            alignment: Alignment.center,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                widget.showBackButton ?? true
                    ? MkBackWidget(
                  img: widget.leadingImg,
                  leadingWidget: widget.leadingWidget,
                  onBack: widget.onBack,
                )
                    : SizedBox(width: 32.w, height: 32.w),
                Expanded(
                  child: Text(
                    widget.title ?? '',
                    textAlign: TextAlign.center,
                    style:
                    widget.titleStyle ??
                        TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold, color: widget.textColor),
                  ),
                ),
                // Spacer 占据剩余空间
                widget.trailingWidget ?? SizedBox(width: 32.w),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class MkBackWidget extends StatelessWidget {
  final String? img;
  final Widget? leadingWidget;
  final Function? onBack;

  const MkBackWidget({super.key, this.img, this.leadingWidget, this.onBack});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 32.w,
      height: 32.w,
      child: GestureDetector(
        onTap: () {
          if (onBack != null) {
            onBack!.call();
          } else {
            Get.back();
          }
        },
        child: leadingWidget ?? Image.asset(img ?? Assets.assetsMkAppbarBack, width: 24.w, height: 24.w),
      ),
    );
  }
}
